package com.ruoyi.web.controller.pc.uc;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.validator.group.AddGroup;
import com.ruoyi.common.validator.group.UpdateGroup;
import com.ruoyi.web.domain.UcArea;
import com.ruoyi.web.domain.vo.QueryVo;
import com.ruoyi.web.service.IUcAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


@RestController
@RequestMapping("/userConfig/ucArea")
public class UcAreaController extends BaseController {

    @Autowired
    private IUcAreaService ucAreaService;

    @PreAuthorize("@ss.hasPermi('userConfig:ucArea:list')")
    @GetMapping("/list")
    public AjaxResult list(QueryVo queryVo) {
        List<UcArea> list = ucAreaService.listBy(queryVo);
        return success(list);
    }


    @PreAuthorize("@ss.hasPermi('userConfig:ucArea:add')")
    @Log(title = "区划管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated(AddGroup.class) @RequestBody UcArea ucArea){

        if (!ucAreaService.repeatCheck(ucArea)){
            return error("新增区划[" + ucArea.getCode() +"]" + ucArea.getName() + "失败，编号或名称已存在");
        }

        return toAjax(ucAreaService.insert(ucArea));
    }


    @GetMapping(value = "/info/{id}")
    public AjaxResult info(@PathVariable("id") Long id){
        UcArea ucArea = ucAreaService.getById(id);
        return success(ucArea);
    }


    @PreAuthorize("@ss.hasPermi('userConfig:ucArea:edit')")
    @Log(title = "区划管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated(UpdateGroup.class) @RequestBody UcArea ucArea){
        if (!ucAreaService.repeatCheck(ucArea)){
            return error("修改区划[" + ucArea.getCode() +"]" + ucArea.getName() + "失败，编号或名称已存在");
        }
        if (ucArea.getId()==ucArea.getParentId()){
            return error("修改区划[" + ucArea.getCode() +"]" + ucArea.getName() + "失败，上级不能是自己");
        }


        return toAjax(ucAreaService.update(ucArea));
    }


    @PreAuthorize("@ss.hasPermi('userConfig:ucArea:remove')")
    @Log(title = "区划管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        return toAjax(ucAreaService.delete(id));
    }

    @Log(title = "区划管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('userConfig:ucArea:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, QueryVo queryVo){
        List<UcArea> list = ucAreaService.listBy(queryVo);
        ExcelUtil<UcArea> util = new ExcelUtil<>(UcArea.class);
        util.exportExcel(response, list, "区划数据");
    }
}

